package edu.yctc.delivery.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import edu.yctc.delivery.constant.ResultCode;
import edu.yctc.delivery.dao.DeliveryDAO;
import edu.yctc.delivery.dao.DeliveryPointDAO;
import edu.yctc.delivery.dao.TruckDAO;
import edu.yctc.delivery.dao.UserDAO;
import edu.yctc.delivery.entity.DeliveryDO;
import edu.yctc.delivery.entity.DeliveryPointDO;
import edu.yctc.delivery.entity.ResultDO;
import edu.yctc.delivery.entity.TruckDO;
import edu.yctc.delivery.entity.UserDO;
import edu.yctc.delivery.service.MetadataService;
import edu.yctc.delivery.vo.DeliveryVO;

/**
 * MetadataService接口的实现
 * 
 *
 * @author 渠立庆
 */
@ComponentScan({"edu.yctc.delivery.dao"})
@Service("metadataService")
public class MetadataServiceImpl implements MetadataService {

    private final static Logger LOG = LoggerFactory.getLogger("serviceLogger");

    @Resource
    private DeliveryDAO deliveryDAO;

    @Resource
    private DeliveryPointDAO deliveryPointDAO;

    @Resource
    private UserDAO userDAO;

    @Resource
    private TruckDAO truckDAO;

    @Override
    public ResultDO<List<DeliveryVO>> getDeliveryVOsByName(String userName) {
        if (StringUtils.isBlank(userName)) {
            LOG.error("get deliveryVOs by userName fail, parameter invalid, name={}", userName);
            return new ResultDO<List<DeliveryVO>>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID,
                null);
        }
        try {
            List<DeliveryDO> deliveryDOs =
                deliveryDAO.getDeliveryDOsByUserId(userDAO.getUserDOByName(userName).getId());
            List<DeliveryVO> deliveryVOs = new ArrayList<>();
            for (DeliveryDO deliveryDO : deliveryDOs) {
                DeliveryVO deliveryVO = new DeliveryVO();
                deliveryVO.setId(deliveryDO.getId());
                deliveryVO.setDeliverymanName(userDAO.getUserDOById(deliveryDO.getUserId()).getName());
                deliveryVO.setTruckNumber(truckDAO.getTruckDOById(deliveryDO.getTruckId()).getNumber());
                deliveryVO
                    .setDeliveryPointName(deliveryPointDAO.getDeliveryPointDOById(deliveryDO.getPointId()).getName());
                deliveryVOs.add(deliveryVO);
            }
            LOG.info("get deliveryVOs by userName success, name={}, deliveryVOs={}", userName, deliveryVOs);
            return new ResultDO<List<DeliveryVO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryVOs);
        } catch (Exception e) {
            LOG.error("get deliveryVOs by userName error, name={}", userName, e);
            return new ResultDO<List<DeliveryVO>>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<List<DeliveryVO>> getDeliveryVOsByUserId(long userId) {
        if (userId <= 0) {
            LOG.error("get deliveryVOs by userId fail, parameter invalid, userId={}", userId);
            return new ResultDO<List<DeliveryVO>>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID,
                null);
        }
        try {
            List<DeliveryDO> deliveryDOs = deliveryDAO.getDeliveryDOsByUserId(userId);
            List<DeliveryVO> deliveryVOs = new ArrayList<>();
            for (DeliveryDO deliveryDO : deliveryDOs) {
                DeliveryVO deliveryVO = new DeliveryVO();
                deliveryVO.setId(deliveryDO.getId());
                deliveryVO.setDeliverymanName(userDAO.getUserDOById(userId).getName());
                deliveryVO.setTruckNumber(truckDAO.getTruckDOById(deliveryDO.getTruckId()).getNumber());
                deliveryVO
                    .setDeliveryPointName(deliveryPointDAO.getDeliveryPointDOById(deliveryDO.getPointId()).getName());
                deliveryVOs.add(deliveryVO);
            }
            LOG.info("get deliveryVOs by userId success, id={}, deliveryVOs={}", userId, deliveryVOs);
            return new ResultDO<List<DeliveryVO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryVOs);
        } catch (Exception e) {
            LOG.error("get deliveryVOs by userId error, id={}", userId, e);
            return new ResultDO<List<DeliveryVO>>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<List<DeliveryDO>> getAllDeliveryDOs() {
        try {
            List<DeliveryDO> deliveryDOs = deliveryDAO.getAllDeliveryDO();
            LOG.info("get all delivery success");
            return new ResultDO<List<DeliveryDO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryDOs);
        } catch (Exception e) {
            LOG.error("get all deliveryDO error", e);
            return new ResultDO<List<DeliveryDO>>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<List<DeliveryDO>> getDeliveryDOByUserName(String username) {
        if (StringUtils.isBlank(username)) {
            LOG.error("get DelivertDO by name fail, parameter invalid, username={}", username);
            return new ResultDO<List<DeliveryDO>>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID,
                null);
        }
        try {
            UserDO getUserDOByName = userDAO.getUserDOByName(username);
            long userid = getUserDOByName.getId();
            List<DeliveryDO> deliveryDOs = deliveryDAO.getDeliveryByUserId(userid);
            LOG.info("get delivery by username success,username={}", username);
            return new ResultDO<List<DeliveryDO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryDOs);
        } catch (Exception e) {
            LOG.error("get deliveryDO by name error,username={}", e, username);
            return new ResultDO<List<DeliveryDO>>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<DeliveryVO> getDeliveryVOByDeliveryDO(DeliveryDO deliveryDO) {
        if (deliveryDO.getId() <= 0 || deliveryDO.getVolume() <= 0 || deliveryDO.getPointId() <= 0
            || deliveryDO.getUserId() <= 0 || deliveryDO.getTruckId() <= 0) {
            LOG.error("get DelivertDO by name fail, parameter invalid, deliveryDO={}", deliveryDO);
            return new ResultDO<DeliveryVO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID,
                null);
        }
        try {
            DeliveryVO deliveryVO = new DeliveryVO();
            deliveryVO.setId(deliveryDO.getId());
            deliveryVO.setVolume(deliveryDO.getVolume());
            deliveryVO.setDeliveryPointName(deliveryPointDAO.getDeliveryPointDOById(deliveryDO.getPointId()).getName());
            deliveryVO.setPosition(deliveryPointDAO.getDeliveryPointDOById(deliveryDO.getPointId()).getPosition());
            deliveryVO.setUnloadTimeLimit(
                deliveryPointDAO.getDeliveryPointDOById(deliveryDO.getPointId()).getUnloadTimeLimit());
            deliveryVO.setTruckNumber(truckDAO.getTruckDOById(deliveryDO.getPointId()).getNumber());
            deliveryVO.setArriveTime(deliveryDO.getArriveTime());
            LOG.info("get deliveryVO by deliveryDO success,deliveryDO={},deliveryVO={}", deliveryVO, deliveryDO);
            return new ResultDO<DeliveryVO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryVO);
        } catch (Exception e) {
            LOG.error("get deliveryVO by deliveryDO error,deliveryDO={}", e, deliveryDO);
            return new ResultDO<DeliveryVO>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<Void> insertDeliveryPointDO(DeliveryPointDO deliveryPointDO) {
        if (StringUtils.isBlank(deliveryPointDO.getName()) || StringUtils.isBlank(deliveryPointDO.getPosition())) {
            LOG.error("insert deliveryPointDO fail, parameter invalid, deliveryPointDO={}", deliveryPointDO);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            // 检验name唯一索引
            DeliveryPointDO deliveryPointDOByName =
                deliveryPointDAO.getDeliveryPointDOByName(deliveryPointDO.getName());
            if (deliveryPointDOByName != null) {
                LOG.error("insert deliveryPoint fail, already exist th deliveryPoint, name={}, deliveryPointDO={}",
                    deliveryPointDOByName.getName(), deliveryPointDOByName);
                return new ResultDO<>(false, ResultCode.ALREADY_EXIST_THE_DELIVERYPOINT,
                    ResultCode.MSG_ALREADY_EXIST_THE_DELIVERYPOINT);
            }
            deliveryPointDAO.insert(deliveryPointDO);
            LOG.info("insert deliveryPoint success, deliveryPointDO={}", deliveryPointDO);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("insert delivetyPoint error, DeliveryPointDO={}", deliveryPointDO, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> deleteDeliveryPointDO(long deliveryPointId) {
        if (deliveryPointId <= 0) {
            LOG.error("delete deliveryPoint fail, parameter invalid, deliveryId={}", deliveryPointId);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            DeliveryPointDO deliveryPointDOById = deliveryPointDAO.getDeliveryPointDOById(deliveryPointId);
            if (deliveryPointDOById == null) {
                LOG.error(
                    "delete deliveryPoint fail, no such deliveryPoint in database, deliveryPointDO={}, deliveryPointId={}",
                    deliveryPointDOById, deliveryPointId);
                return new ResultDO<>(false, ResultCode.NO_SUCH_DELIVERYPOINT, ResultCode.MSG_NO_SUCH_DELIVERYPOINT);
            }
            deliveryPointDAO.deleteById(deliveryPointId);
            LOG.info("delete deliveryPoint success, deliveryPointDO={}", deliveryPointDOById);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("delete deliveryPoint error, system error", e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> updateDeliveryPointDO(DeliveryPointDO deliveryPointDO) {
        if (StringUtils.isBlank(deliveryPointDO.getName()) || StringUtils.isBlank(deliveryPointDO.getPosition())
            || deliveryPointDO.getId() <= 0) {
            LOG.error("update deliveryPoint fail, parameter invalid,deliveryPointDO={}", deliveryPointDO);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            DeliveryPointDO deliveryPointDOById = deliveryPointDAO.getDeliveryPointDOById(deliveryPointDO.getId());
            if (deliveryPointDOById == null) {
                LOG.error("update deliveryPoint fail, no such deliveryPoint, deliveryPointDO={}", deliveryPointDO);
                return new ResultDO<>(false, ResultCode.NO_SUCH_DELIVERYPOINT, ResultCode.MSG_NO_SUCH_DELIVERYPOINT);
            }
            deliveryPointDAO.update(deliveryPointDO);
            LOG.error("update deliveryPoint success, deliveryPointDO={}", deliveryPointDO);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("update deliveryPoint error,system error, deliveryPoint={}", deliveryPointDO, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> insertTruckDO(TruckDO truckDO) {
        if (StringUtils.isBlank(truckDO.getNumber()) || truckDO.getCost() <= 0 || truckDO.getMaxDistance() <= 0
            || truckDO.getSpeed() <= 0 || truckDO.getCapacity() <= 0) {
            LOG.error("insert truckDO fail, parameter invalid truckDO={}", truckDO);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            // 检验number唯一索引
            TruckDO truckDOByNumber = truckDAO.getTruckDOByNumber(truckDO.getNumber());
            if (truckDOByNumber != null) {
                LOG.error("insert truckDO fail ,the truck already exist,truckNumber={}, truckDO={}",
                    truckDO.getNumber(), truckDO);
                return new ResultDO<>(false, ResultCode.ALREADY_EXIST_THE_TRUCK,
                    ResultCode.MSG_ALREADY_EXIST_THE_TRUCK);
            }
            truckDAO.insert(truckDO);
            LOG.info("insert truckDO success,truckDO={}", truckDO);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("insert truckDO error,truckDO={}", truckDO, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> deleteTruckDO(long truckId) {
        if (truckId <= 0) {
            LOG.error("delete truckDO fail, parameter invalid, truckId={}", truckId);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            TruckDO truckDOById = truckDAO.getTruckDOById(truckId);
            if (truckDOById == null) {
                LOG.error("delete truckDO fail, no such truck in database, truckId={}, truckDO={}", truckId,
                    truckDOById);
                return new ResultDO<>(false, ResultCode.NO_SUCH_TRUCK, ResultCode.MSG_NO_SUCH_TRUCK);
            }
            truckDAO.deleteById(truckId);
            LOG.info("delete truck success, truckId={}, truckDO={}", truckId, truckDOById);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("delete truck error, truckId={}", truckId, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> updateTruckDO(TruckDO truckDO) {
        if (StringUtils.isBlank(truckDO.getNumber()) || truckDO.getCapacity() <= 0 || truckDO.getCost() <= 0
            || truckDO.getMaxDistance() <= 0 || truckDO.getSpeed() <= 0) {
            LOG.error("update truckDO fail, parameter invalid, truckDO={}", truckDO);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            TruckDO truckDOById = truckDAO.getTruckDOById(truckDO.getId());
            if (truckDOById == null) {
                LOG.error("update truckDO fail, no such truck, truckId={}, truckDO={}", truckDO.getId(), truckDO);
                return new ResultDO<>(false, ResultCode.NO_SUCH_TRUCK, ResultCode.MSG_NO_SUCH_TRUCK);
            }
            truckDAO.update(truckDO);
            LOG.info("update truckDO success, truckDO={}", truckDO);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("update truckDO error, truckDO={}", truckDO, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<DeliveryPointDO> getDeliveryPointDOById(long deliveryPointId) {
        if (deliveryPointId <= 0) {
            LOG.error("get deliveryPoint by id fail, parameter invaild,deliveryPointId={}", deliveryPointId);
            return new ResultDO<DeliveryPointDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID,
                null);
        }
        try {
            DeliveryPointDO deliveryPointDOById = deliveryPointDAO.getDeliveryPointDOById(deliveryPointId);
            if (deliveryPointDOById == null) {
                LOG.error("get deliveryPoint by id fail, no such deliveryPoint in database, deliveryPointDO={}",
                    deliveryPointDOById);
                return new ResultDO<DeliveryPointDO>(false, ResultCode.NO_SUCH_DELIVERYPOINT,
                    ResultCode.MSG_NO_SUCH_DELIVERYPOINT, null);
            }
            LOG.info("get deliveryPoint by id success, deliveryPointDO={}", deliveryPointDOById);
            return new ResultDO<DeliveryPointDO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS,
                deliveryPointDOById);
        } catch (Exception e) {
            LOG.error("get deliveryPoint by id error, deliveryPointId={}", deliveryPointId, e);
            return new ResultDO<DeliveryPointDO>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<TruckDO> getTruckDOById(long truckId) {
        if (truckId <= 0) {
            LOG.error("get truckDO by id fail, parameter invaild, truckId={}", truckId);
            return new ResultDO<TruckDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        try {
            TruckDO truckDOById = truckDAO.getTruckDOById(truckId);
            if (truckDOById == null) {
                LOG.error("get truckDO by id fail, no such truckDO in database, truckId={}, truckDO={}", truckId,
                    truckDOById);
                return new ResultDO<TruckDO>(false, ResultCode.NO_SUCH_TRUCK, ResultCode.MSG_NO_SUCH_TRUCK, null);
            }
            LOG.info("get truckDO by id success, truckDO={}", truckDOById);
            return new ResultDO<TruckDO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, truckDOById);
        } catch (Exception e) {
            LOG.error("get truckDO by id error, truckId={}", truckId);
            return new ResultDO<TruckDO>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                ResultCode.MSG_ERROR_SYSTEM_EXCEPTION, null);
        }
    }

    @Override
    public ResultDO<Void> deleteDeliveryDOsByUserId(long userId) {
        if (userId <= 0) {
            LOG.error("delete deliveryDO by user id fail, parameter invaild, userId={}", userId);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            // 是否有该配送员
            UserDO userDOById = userDAO.getUserDOById(userId);
            if (userDOById == null) {
                LOG.error("delete deliveryDO by user id fail, no such user for deliveryDO, userId={}, userDO={}",
                    userId, userDOById);
                return new ResultDO<>(false, ResultCode.NO_SUCH_USER, ResultCode.MSG_NO_SUCH_USER);
            }
            List<DeliveryDO> deliveryDOsByUserId = deliveryDAO.getDeliveryDOsByUserId(userId);
            // 该配送员id是否有对应的订单
            if (deliveryDOsByUserId.size() > 0) {
                deliveryDAO.deleteByUserId(userId);
                LOG.info("delete deliveryDO by user id success, userId={}", userId);
                return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
            } else {
                LOG.error("no such deliveryDO by user id, userId={}", userId);
                return new ResultDO<>(false, ResultCode.NO_SUCH_DELIVERY, ResultCode.MSG_NO_SUCH_DELIVERY);
            }
        } catch (Exception e) {
            LOG.error("delete deliveryDO by user id error,userId={}", userId, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> deleteDeliveryDOByUserIdAndDeliveryPointId(long userId, long deliveryPointId) {
        if (userId <= 0 || deliveryPointId <= 0) {
            LOG.error(
                "delete deliveryDO by userId and deliveryPointId fail, parameter invalid, userId={}, deliveryPointId={}",
                userId, deliveryPointId);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            // 是否有该配送员
            UserDO userDOById = userDAO.getUserDOById(userId);
            if (userDOById == null) {
                LOG.error("delete deliveryDO by userId and deliveryPointId fail, no such user, userId={}, userDO={}",
                    userId, userDOById);
                return new ResultDO<>(false, ResultCode.NO_SUCH_USER, ResultCode.MSG_NO_SUCH_USER);
            }
            // 是否有该配送地点
            DeliveryPointDO deliveryPointDOById = deliveryPointDAO.getDeliveryPointDOById(deliveryPointId);
            if (deliveryPointDOById == null) {
                LOG.error(
                    "delete deliveryDO by userId and deliveryPointId fail, no such deliverypoint ,deliveryPointId={}, deliveryPointDO={}",
                    deliveryPointId, deliveryPointDOById);
                return new ResultDO<>(false, ResultCode.NO_SUCH_DELIVERYPOINT, ResultCode.MSG_NO_SUCH_DELIVERYPOINT);
            }
            // 是否有该订单
            DeliveryDO deliveryDO = deliveryDAO.getDeliveryDOByUserIdAndDeliveryPointId(userId, deliveryPointId);
            if (deliveryDO == null) {
                LOG.error(
                    "delete deliveryDO by userId and deliveryPointId fail, no such delivery, userId={}, deliverPointId={}, deliveryDO={}",
                    userId, deliveryPointId, deliveryPointDOById);
                return new ResultDO<>(false, ResultCode.NO_SUCH_DELIVERY, ResultCode.MSG_NO_SUCH_DELIVERY);
            }
            deliveryDAO.deleteByUserIdAndDeliveryPointId(userId, deliveryPointId);
            LOG.info("delete deliveryDO by userId and deliveryPointId success, userId={}, deliveryPointId={}", userId,
                deliveryPointId);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("delete deliveryDO by userId and deliveryPointId error, userId={}, deliveryPointId={}", userId,
                deliveryPointId, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

    @Override
    public ResultDO<Void> insertDeliveryDOs(List<DeliveryVO> deliveryVOs) {
        if (deliveryVOs.size() > 0) {
            for (int i = 0; i < deliveryVOs.size(); i++) {
                // 先将DeliveryVO封装为DeliveryDO
                DeliveryVO deliveryVO = new DeliveryVO();
                deliveryVO = deliveryVOs.get(i);
                DeliveryDO deliveryDO = new DeliveryDO();
                deliveryDO
                    .setPointId(deliveryPointDAO.getDeliveryPointDOByName(deliveryVO.getDeliveryPointName()).getId());
                deliveryDO.setTruckId(truckDAO.getTruckDOByNumber(deliveryVO.getTruckNumber()).getId());
                deliveryDO.setUserId(userDAO.getUserDOByName(deliveryVO.getDeliverymanName()).getId());
                deliveryDO.setArriveTime(deliveryVO.getArriveTime());
                deliveryDO.setVolume(deliveryVO.getVolume());
                // TODO 车上的货物量和到达配送点的时间处理
                // 将封装后的deliveryDO录入数据库
                if (deliveryDO.getPointId() <= 0 || deliveryDO.getTruckId() <= 0 || deliveryDO.getUserId() <= 0) {
                    LOG.error("insert deliveryDO fail, parameter invalid, deliveryDO={}, correspond deliveryVOs[i={}]",
                        deliveryDO, i);
                    return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
                }
                try {
                    DeliveryDO deliveryDOByUserIdAndDeliveryPointId =
                        deliveryDAO.getDeliveryDOByUserIdAndDeliveryPointId(deliveryDO.getUserId(),
                            deliveryDO.getPointId());
                    if (deliveryDOByUserIdAndDeliveryPointId != null) {
                        LOG.error(
                            "insert deliveryDO fail, already exist the deliveryDO in database, deliveryDO={},correspond deliveryVOs[i={}]",
                            deliveryDO, i);
                        return new ResultDO<>(false, ResultCode.ALREADY_EXIST_THE_DELIVERY,
                            ResultCode.MSG_ALREADY_EXIST_THE_DELIVERY);
                    }
                    deliveryDAO.insert(deliveryDO);
                    LOG.info("insert deliveryDO success, deliveryDO={}, correspond deliveryVOs[i={}]", deliveryDO, i);
                } catch (Exception e) {
                    LOG.error("insert deliveryDO error, deliveryDO={}, correspond deliveryVOs[i={}]", deliveryDO, i, e);
                    return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION,
                        ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
                }
            }
        } else {
            LOG.error("insert deliveryDOs fail, parameter no deliverys to insert, deliveryVOs={}", deliveryVOs);
            return new ResultDO<>(false, ResultCode.NO_DELIVERYS_TO_INSERT, ResultCode.MSG_NO_DELIVERYS_TO_INSERT);
        }
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
    }

}
